package com.example.activiti.controller;

import com.example.activiti.modle.JsonResult;
import com.example.activiti.service.ProcessUtilService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: 程荣凯
 * @Date: 2018/9/21 15:08
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    ProcessUtilService processService;

    /**
     * 发布流程定义
     * @param defName 流程定义名称
     * @param defImage 流程定义图片
     * @return 发布结果
     */
    @RequestMapping("/publishDef")
    public String publishDef(String defName,String defImage){
        boolean result = processService.publishProcess(defName,defImage);
        if (result){
            return "发布成功";
        }else{
            return "发布失败";
        }
    }

    /**
     * 发布所有流程定义
     * @return 发布结果
     */
    @RequestMapping("/publishAllDef")
    public String publishAllDef(){
        boolean result = processService.publishProcessAll();
        if (result){
            return "发布成功";
        }else{
            return "发布失败";
        }
    }

    /**
     * 根据流程定义ID创建流程实例
     * @param processDefId 流程定义ID
     * @return 流程启动结果
     */
    @RequestMapping("/startProcessInstById")
    public String startProcessInstById(String processDefId){
        String processInstId = processService.startProcessInstById(processDefId);
        if (processInstId != null){
            return "流程启动成功,"+"流程实例ID为"+processInstId;
        }else {
            return "流程启动失败";
        }
    }

    /**
     * 根据流程定义KEY创建流程实例
     * @param processDefKey 流程定义KEY
     * @return 流程启动结果
     */
    @RequestMapping("/startProcessInstByKey")
    public String startProcessInstByKey(String processDefKey){
        String processInstId = processService.startProcessInstByKey(processDefKey);
        if (processInstId != null){
            return "流程启动成功,"+"流程实例ID为"+processInstId;
        }else {
            return "流程启动失败";
        }
    }

    /**
     * 完成工作项
     * @param processInstId 流程实例ID
     * @param workItemId 工作项ID
     * @return 完成工作项结果
     */
    @RequestMapping("/finishWorkItem")
    public String finishWorkItem(String processInstId,String workItemId){
        String userId = null;
        boolean result = processService.finishWorkItem(processInstId,workItemId,userId);
        if (result){
            return "完成工作项"+workItemId+"成功";
        }else {
            return "完成工作项"+workItemId+"失败";
        }
    }

    /**
     * 回退工作项
     * @param processInstId 流程实例ID
     * @param workItemId 工作项ID
     * @return 回退工作项结果
     */
    @RequestMapping("/backWorkItem")
    public String backWorkItem(String processInstId,String workItemId){
        String userId = null;
        boolean result = processService.backWorkItem(processInstId,workItemId,userId);
        if (result){
            return "回退工作项"+workItemId+"成功";
        }else {
            return "回退工作项"+workItemId+"失败";
        }
    }

    /**
     * 设置工作项的参与者
     * @param processInstId 流程实例ID
     * @param workItemId 工作项ID
     * @param participant 参与者ID
     * @param type 参与者类型(用户/用户组)
     * @return 设置参与者结果
     */
    @RequestMapping("/setParticipant")
    public String setParticipant(String processInstId,String workItemId,String participant,String type){
        boolean result;
        if (type.equals(0)){
            result = processService.setAssigner(processInstId,workItemId,participant);
        }else if(type.equals(1)){
            result = processService.setCandidateUser(processInstId,workItemId,participant);
        }else {
            result = processService.setCandidateGroup(processInstId,workItemId,participant);
        }
        if (result){
            return "设置参与者"+participant+"成功";
        }else {
            return "设置参与者"+participant+"失败";
        }
    }

    /**
     * 根据用户ID查询待办工作项
     * @param userId 用户ID
     * @return 工作项列表
     */
    @RequestMapping("/queryUndoTask")
    public JsonResult queryUndoTask(String userId){
        List<Task> taskList = processService.queryUndoTask(userId);
        return new JsonResult("0",taskList);
    }

    /**
     * 根据ID查询用户已办工作项
     * @param userId 用户ID
     * @return 工作项列表
     */
    @RequestMapping("/queryDoneTask")
    public JsonResult queryDoneTask(String userId){
        List<HistoricTaskInstance> taskList = processService.queryDoneTask(userId);
        return new JsonResult("0",taskList);
    }

    /**
     * 根据ID查询用户已办结工作项
     * @param userId 用户ID
     * @return 工作项列表
     */
    @RequestMapping("/queryFinishTask")
    public JsonResult queryFinishTask(String userId){
        List<HistoricTaskInstance> taskList = processService.queryFinishTask(userId);
        return new JsonResult("0",taskList);
    }

}
